<div class="layui-row">
  <div class="layui-col-lg3">
    <div class="layui-form-item">
      <div class="layui-inline">
        <button class="layui-btn" onclick="new_warehouse()">新增入库</button>
      </div>
      <div class="layui-inline">
        <button class="layui-btn" onclick="out_warehouse('out_storage')">出库</button>
      </div>
      <div class="layui-inline">
        <button class="layui-btn" onclick="out_warehouse('lend')">领用</button>
      </div>
      <div class="layui-inline">
        <button class="layui-btn" onclick="back_warehouse()">归还</button>
      </div>
    </div>
  </div>
  <div class="layui-col-lg9">
    <form class="layui-form layui-form-pane" action="">
      <div class="layui-form-item">
        <div class="layui-inline">
          <div class="layui-input-block">
            <input type="text" id="current_warehouse_name" list="current_warehouse" placeholder="库存位置选择"  autocomplete="off" class="layui-input" onkeyup="show_warehouse_asset(this.value)">
            <datalist id="current_warehouse"></datalist>
          </div>
        </div>
        <div class="layui-inline">
          <!-- <label class="layui-form-label layui-bg-green">品类查询</label> -->
          <div class="layui-input-inline">
            <input type="text" name="asset_kind" list="asset_kind_chose" placeholder="品类选择"  autocomplete="off" class="layui-input" onkeyup="select_asset_kind(this.value,'query')">
            <datalist id="asset_kind_chose"></datalist>
          </div>
        </div>
        <div class="layui-inline">
          <div class="layui-input-inline">
            <input type="text" l name="search_key"  placeholder="编号/名称" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <a class="layui-btn" lay-submit="" lay-filter="data-search-btn">搜索</a>
        </div>
      </div>
    </form>
  </div>
</div>

<!-- 添加数据表单开始 -->
<form  class="layui-form layui-form-pane " action="" id="handle_data_form" lay-filter="handle_data_form" style="padding: 10px;display: none;">


  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">品类</label>
      <div class="layui-input-block">
        <input type="text"  list="leader_kind" name="kind" lay-verify="required" lay-reqtext="必填项!"  placeholder="必选，选择品类"  autocomplete="off" class="layui-input">
        <datalist id="leader_kind"></datalist>
      </div>
    </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">名称</label>
      <div class="layui-input-block">
        <input type="text" name="name" placeholder="自定义名称" autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">规格描述</label>
      <div class="layui-input-block">
        <input type="text" name="spec"  placeholder="规格描述"  autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">数量</label>
      <div class="layui-input-block">
        <input type="text" name="number" lay-verify="number" value="1"  placeholder="数量"  autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">单位</label>
      <div class="layui-input-block">
        <input type="text" name="unit" placeholder="单位"  autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>

   <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">位置</label>
      <div class="layui-input-block">
        <input type="text"  list="storage_loca" name="location" lay-verify="required" placeholder="选择/输入库存位置"  autocomplete="off" class="layui-input">
        <datalist id="storage_loca"></datalist>
      </div>
    </div>
  </div>

  <div class="layui-form-item" >
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green" >绑定标签ID</label>
      <div class="layui-input-block" >
        <input id="bind_device" name="device_id" type="text" list="bind_device_list"  name="tml_serch"  placeholder="选择设备" autocomplete="off" class="layui-input">
          <datalist id="bind_device_list"></datalist>
      </div>
    </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">交接人</label>
      <div class="layui-input-block">
        <input type="text" list="person_list" name="person"  placeholder="交接人" placeholder="交接人"  autocomplete="off" class="layui-input"> 
        <datalist id="person_list"></datalist>
      </div>
    </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">备注</label>
      <div class="layui-input-block">
        <input type="text" name="remark"  placeholder="备注"  autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>

  <div class="layui-form-item" style="display: none;">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">状态</label>
      <div class="layui-input-block">
        <input type="text" name="status" placeholder="单位" value="on"  autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>

<!--   <div class="layui-form-item" style="display: none;">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">添加方式</label>
      <div class="layui-input-block">
        <input type="text" name="in_way" placeholder="单位" value="rfid_input"  autocomplete="off" class="layui-input">
      </div>
    </div>
  </div> -->

  <!-- <button class="layui-btn layui-btn-fluid" id="add_submit_btn" lay-submit lay-filter="add_submit" >提交</button> -->

</form>

<!-- 添加数据表单结束 -->

<table class="layui-table" id="base_info_manage_table" lay-filter="base_info_manage_table"></table>
<!--入库表头 -->
<script type="text/html" id="storage_table_header">
    <a class="layui-btn layui-btn-xs data-count-edit" lay-event="refresh">刷新</a>
    <a class="layui-btn layui-btn-xs data-count-edit" lay-event="batch_edit">批量编辑</a>
    <a class="layui-btn layui-btn-xs data-count-edit" lay-event="all_edit">全部编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-warm " lay-event="add_cols">新增行</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger " lay-event="delete">删除行</a>
</script>
<!-- 出库表头 -->
<!-- <script type="text/html" id="out_table_header">
    <a class="layui-btn layui-btn-xs data-count-edit" lay-event="refresh">刷新</a>
    <a class="layui-btn layui-btn-xs layui-btn-warm " lay-event="add_cols">新增行</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger " lay-event="delete">删除行</a>
    <input type="text" name="asset_kind" list="asset_kind_out" placeholder="品类选择"  autocomplete="off" class="layui-inline" onkeyup="select_asset_kind(this.value)">
    <datalist id="asset_kind_out"></datalist>
    <input class="layui-inline" type="text" placeholder="输入名称/编号进行搜索" id="query_out_store" style="display: inline;">
    <a class="layui-btn layui-btn-xs"  lay-event="query">搜索</a>
</script> -->


<!-- 入库操作表 -->
<div id="in_storage" style="display: none;padding: 10px">
  <table class="layui-table" id="storage_table" lay-filter="storage_table"></table>
</div>
<!-- 出库操作表 -->
<div id="out_storage_panle" style="display: none;padding: 10px;text-align: center;">
  <form class="layui-form" action="" id="out_sotrage_form" lay-filter="out_sotrage_form">
    <div class="layui-form-item">
<!--       <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">出库类型</label>
        <div class="layui-input-block">
          <input type="radio" name="out_kind" value="lend" title="借出" checked="" autocomplete="off" class="layui-input">
          <input type="radio" name="out_kind" value="out_storage" title="出库" autocomplete="off" class="layui-input">
        </div>
      </div> -->

      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">领用人</label>
        <div class="layui-input-block">
          <input type="text" name="person"  list="handle_person_list" placeholder="资产领用人" autocomplete="off" class="layui-input">
          <datalist id="handle_person_list"></datalist>
        </div>
      </div>

      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">备注</label>
        <div class="layui-input-block">
          <input type="text" name="remark"  placeholder="备注"  autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-inline">
        <div class="layui-input-block">
          <input type="checkbox" name="dismiss" title="标签回收" placeholder="备注" autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-inline" style="display: none;">
        <label class="layui-form-label layui-bg-green">扫描ID</label>
        <div class="layui-input-block">
          <input type="text" name="scann_id"  placeholder="现场扫描的标签ID"  autocomplete="off" class="layui-input">
        </div>
      </div>
    </div>
  </form>
  <div id="out_storage" class="layui-transfer" style="padding: 10px"></div>
</div>

<!-- 归还操作表 -->
<div id="back_storage_panle" style="display: none;padding: 10px;text-align: center;">
  <form class="layui-form" action="" id="back_storage_form" lay-filter="back_storage_form">
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">归还人</label>
        <div class="layui-input-block">
          <input type="text" name="person"  list="back_person_list" placeholder="资产归还人" autocomplete="off" class="layui-input">
          <datalist id="back_person_list"></datalist>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">备注</label>
        <div class="layui-input-block">
          <input type="text" name="remark"  placeholder="备注"  autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-inline" style="display: none;">
        <label class="layui-form-label layui-bg-green">扫描ID</label>
        <div class="layui-input-block">
          <input type="text" name="scann_id"  placeholder="现场扫描的标签ID"  autocomplete="off" class="layui-input">
        </div>
      </div>
    </div>
  </form>
  <div id="back_storage" class="layui-transfer" style="padding: 10px"></div>
</div>

<script type="text/html" id="chose_asset_kind">
    <a class="layui-btn layui-btn-xs " lay-event="select">编辑</a>
</script>
<!-- 批量导入处理开始 -->
<div class="layui-form-item" style="display: none;">
    <div class="layui-inline">
      <label class="layui-form-label layui-bg-green">id</label>
      <div class="layui-input-block">
        <input type="text" name="device_input"  autocomplete="off" class="layui-input" id="device_input">
      </div>
    </div>
</div>
<!-- 设备导入处理结束 -->

<!-- 引入把上传的表格转变为json的插件 -->
<!-- <script src="/static/js-xlsx-master/xlsx.js"></script> -->
<!-- js代码开始 -->

<script type="text/javascript">

if(user_permi=='superuser'){
  $('#add_device_permi').show()
  $('#device_input_btn').show()
  $('#device_deliver_permi').show()
}
if(user_permi=='代理商'){
  $('#device_deliver_permi').show()
}
var ajax_url='warehouse';
// 设备数据表格渲染函数

var table_show=function(table_data,div='#base_info_manage_table',toolbar='#table_header'){
  table.render({
    elem:div ,
    toolbar:'<p style="font-size:20px;color:green">资产信息</p>',
    data: table_data,
    height:get_height-200,
    id:'base_info_manage_table',
    cols: [[
        {type: "checkbox", fixed: "left"},
        {field: 'index', title: '序号',width:50,templet:function(obj){
          return obj.LAY_TABLE_INDEX+1
        }},
        {field: 'kind', title: '品类',sort:true},
        {field: 'name', title: '名称',sort:true},
        {field: 'unique_id', title: '编号',sort:true},
        {field: 'spec', title: '规格',sort:true},
        {field: 'number', title: '数量',sort:true},
        {field: 'unit', title: '单位',sort:true,},
        {field: 'device_id', title: '标签位置',sort:true,templet:function(obj){
          var dev=obj.device_id
          var loca=get_detail_from_id(device,dev)
          if(loca){
            return dev+','+loca.dynamic.location
          }
          return dev
        }},
        {field: 'location', title: '初始位置',sort:true},
        {field: 'owner', title: '操作人',sort:true,templet:function(obj){
          return user_info_objs[obj.owner].visible_name
        }},
        {field: 'remark', title: '备注'},
        {field: 'date_added', title: '创建时间',sort:true},
        {title: '操作', minWidth: 110, templet: '#currentTableBar', fixed: "right", align: "center"}
    ]],
    // limits: [10, 15, 20, 25, 50, 100],
    limit: 1000000,
    // page: true
  });
}

var storage_table=function(table_data,toolbar='#storage_table_header'){
 
  table.render({
    elem: '#storage_table',
    toolbar:toolbar,
    data: table_data,
    id:'storage_table',
    cols: [
    [
        {type: "checkbox", fixed: "left"},
        {field: 'index', title: '序号',width:50,templet:function(obj){
          return obj.LAY_TABLE_INDEX+1
        }},
        {field: 'kind', title: '品类',sort:true},
        {field: 'name', title: '自定义名称',sort:true},
        {field: 'spec',edit:'text',title: '规格',sort:true},
        {field: 'number',edit:'text', title: '数量',sort:true},
        {field: 'unit',edit:'text', title: '单位',sort:true},
        {field: 'location',edit:'text', title: '库存位置',sort:true},
        {field: 'device_id', title: '标签编号',sort:true},  
        {field: 'status',edit:'text', title: '当前状态',sort:true},
        {field: 'person',edit:'text', title: '交接人',sort:true},
        {field: 'in_way',edit:'text', title: '入库方式',sort:true},
        {field: 'remark',edit:'text', title: '备注'},
        {title: '操作',sort:true,templet: '#chose_asset_kind', fixed: "right", align: "center"},

    ]],
    // limits: [10, 15, 20, 25, 50, 100],
    limit: 100000,
    // page: true
  });
}


var building_show=function(table_data){
  table.render({
    elem: '#base_info_manage_table',
    toolbar:'<p style="font-size:20px;color:green">建筑信息</p>',
    data: table_data,
    id:'base_info_manage_table',
    cols: [[
        {type: "checkbox", fixed: "left"},
        // {field: 'id', title: 'ID',width:20,sort:true},
        {field: 'name', title: '名称',sort:true},
        {field: 'name_id', title: '编号',sort:true},
        {field: 'position', title: '位置',sort:true},
        // {field: 'kind', title: '类型',sort:true},
        // {field: 'auth_in', title: '进入权限',sort:true,templet:function(d){
        //   var param_set=eval('('+d.param_set+')')
        //   return param_set.auth_in
        // }},
        {field: 'volume', title: '容量',sort:true,templet:function(d){
          var param_set=eval('('+d.param_set+')')
          return ''
        }},
        {field: 'volume_used', title: '已使用',sort:true,templet:function(d){
          var param_set=eval('('+d.param_set+')')
          return ''
        }},
        {field: 'asset', title: '库存资产',sort:true,templet:function(d){
          var param_set=eval('('+d.param_set+')')
          var asset_store=filter_func(asset,'location',d.name)
          return asset_store.length
        }},
        // {field: 'person_limit', title: '人数限制',sort:true,templet:function(d){
        //   var param_set=eval('('+d.param_set+')')
        //   return param_set.person_limit
        // }},
        // {field: 'alarm_time', title: '报警时段',sort:true,templet:function(d){
        //   var param_set=eval('('+d.param_set+')')
        //   return param_set.alarm_time
        // }},
        {field: 'remark', title: '备注'},
        // {field: 'date_added', title: '添加时间',sort:true},
        {title: '操作', minWidth: 110, templet: '#table_info_handle_bar', fixed: "right", align: "center"}
    ]],
    limits: [10, 15, 20, 25, 50, 100],
    limit: 10,
    page: true
  });
}



// 首次用户数据加载
var asset,asset_kind,dev_data,person;
var handle_get_func=function(data){
  console.log(data)
  building=data['building'],scanner=data['scanner'],asset=data['asset'],asset_kind=data['asset_kind'],dev_data=data['dev_data'],person=data['person'];
  building_show(building)
  fill_option(building,'current_warehouse','name')
  $('#current_warehouse').append('<option>总览</option>')
    // 加载搜素栏目的资产类别
  fill_option(asset_kind,'asset_kind_chose','name')
}
get_info_ajax(ajax_url,'get_init_data',handle_get_func)

// 监听仓库列表点击
var current_warehouse='';
table.on('row(base_info_manage_table)', function(obj){
  var res=obj.data
  if(res.position!=null){
    show_warehouse_asset(res.name)
    $('#current_warehouse_name').val(res.name)
  }
});

// 展示指定仓库库存
var show_warehouse_asset=function(name){
  if(name=='总览' |  name==''){
    building_show(building)
    return
  }
  current_warehouse=name
  var asset_obj=filter_func(asset,'location',name)
  table_show(asset_obj)
}

// 根据品类选择展示库存
var select_asset_kind=function(value){
  console.log(value)
  if(value==''){
    return false
  }
  var table_data=table.getData('base_info_manage_table')
  var select_data=[]
  table_data.forEach(item=>{
    if(item.kind==value){
      select_data.push(item)
    }
  })
  table_show(select_data)
}

// 入库信息编辑
var edit_storage=function(obj,kind=''){
  document.getElementById("handle_data_form").reset();
  fill_option(asset_kind,'leader_kind','name')
  fill_option(building,'storage_loca','name')
  fill_option(person,'person_list','name')
  if(current_warehouse){
    form.val('handle_data_form',{'location':current_warehouse})
  }
  layer.open({
      type: 1
      ,title:'资产编辑'
      // ,area:['80%','80%']
      ,offset:'100px'  
      ,id: 'handle_storage_form' //防止重复弹出
      ,content:$('#handle_data_form')
      ,shade: 0 //不显示遮罩
      ,btn:['保存','关闭']
      ,btn1:function(d){
        var asset_data=form.val('handle_data_form')

        var k_data=filter_func(asset_kind,'name',asset_data.kind)
        if(k_data.length==0){
          layer.msg('请选择品类，没有品类，请在品类管理创建')
          return false
        }

        var b_data=filter_func(building,'name',asset_data.location)
        if(b_data.length==0){
          layer.msg('请选择库存位置，没有对应位置，请在建筑管理创建')
          return false
        }

        var p_data=filter_func(person,'name',asset_data.person)
        if(p_data.length==0){
          layer.msg('请选择交接人，没有人员，请在人员管理创建')
          return false
        }

        if(kind=='batch'){
          delete asset_data.device_id
          var current_data=obj.current_data
          var chose_data=obj.chose_data

          var chose_cols=[]
          var new_cols=[]
          chose_data.forEach(item=>{
            chose_cols.push(item.index)
          })
          storage_table(new_cols)
          current_data.forEach(item=>{
            if(chose_cols.indexOf(item.index)!==-1){
              for(var k in asset_data){
                item[k]=asset_data[k]
              }
            }
          })
          storage_table(current_data)
        }else if(kind=='all'){
          delete asset_data.device_id
          obj.forEach(item=>{
            for(var k in asset_data){
              item[k]=asset_data[k]
            }
          })
          storage_table(obj)
        }else{
          obj.update(asset_data)
        }
        layer.close(d)
      }
    });
  // obj.update({'kind':'电脑'})
}

// 单条入库信息编辑
table.on('tool(storage_table)',function(obj){
  edit_storage(obj)
  document.getElementById("handle_data_form").reset();
  var new_dev=[]
  var binded_dev=[]
  var table_data=table.getData('storage_table')
  table_data.forEach(item=>{
    binded_dev.push(item.device_id)
  })
  dev_data.forEach(item=>{
    if(binded_dev.indexOf(item.device_id)==-1){
      new_dev.push(item)
    }
  })
  fill_option(new_dev,'bind_device_list','device_id')
  if(current_warehouse){
    obj.data.location=current_warehouse
  }
  form.val('handle_data_form',obj.data)
})


// 获取出入库扫描读头扫描到的数据
var show_scann_data=function(data){
  asset=data.asset
  var storage_data=data.storage_data
  document.getElementById("handle_data_form").reset();
  var table_data=[]
  for(var i=0;i<storage_data.length;i++){
    var field=form.val('handle_data_form')
    field.device_id=storage_data[i]['device_id']
    field.in_way='rfid'
    table_data.push(field)
  }
  console.log(table_data)
  storage_table(table_data)
  
}
// 入库点击操作
var new_warehouse=function(){
  layer.open({
    type: 1
    ,title:'<p style="font-size:20px;color:green">资产入库</p>'
    ,area:['80%','80%']
    ,offset:'100px'  
    ,id: 'in_storage_panle' //防止重复弹出
    ,content:$('#in_storage')
    ,shade: 0 //不显示遮罩
    ,success:function(){
      post_info_ajax({'warehouse_name':current_warehouse,'kind':'new_add'},ajax_url,'get_scann_data',show_scann_data,'')
    }
    ,btn:['提交','关闭']
    ,btn1:function(d){
      var res=table.getData('storage_table')
      console.log(res)
      var kind_check=false
      var loca_check=false
      var person_check=false
      res.forEach(item=>{
        var k_data=filter_func(asset_kind,'name',item.kind)
        if(k_data.length==0){
          kind_check={'index':item.index}
        }

        var b_data=filter_func(building,'name',item.location)
        if(b_data.length==0){
          loca_check={'index':item.index}
        }

        var p_data=filter_func(person,'name',item.person)
        if(p_data.length==0){
          person_check={'index':item.index}
        }
        item.detail={'in_way':item.in_way,'deliver_person':item.person}
        item.status='in_storage'
        delete item.index
        delete item.in_way
        delete item.person
      })
      if(kind_check){
        layer.msg('第'+kind_check.index+'行品类错误，没有对应品类，请在品类管理创建')
        return false
      }
      if (loca_check){
        layer.msg('第'+loca_check.index+'行库存位置错误，没有对应库存位置，请在建筑管理创建')
        return false
      }

      if (person_check){
        layer.msg('第'+person_check.index+'行人员选择错误，没有对应人员，请在人员管理创建')
        return false
      }
      console.log(res)
      post_info_ajax(res,ajax_url,'new_add',table_show)
      // layer.close(d)
    }
  });
}


// 入库操作的刷新按钮
var refresh_sacnn_data=function(data){
  asset=data.asset
  var storage_data=data.storage_data
  storage_data=storage_data.concat(current_storate_data)
  storage_table(storage_data)
}

var current_storate_data=[];
// 监听入库表格头部按钮操作
table.on('toolbar(storage_table)',function(obj){
  var t_data=obj.config.data;
  current_storate_data=obj.config.data;

  if(obj.event=='refresh'){
    post_info_ajax({'warehouse_name':current_warehouse,'kind':'in_storage'},ajax_url,'get_scann_data',refresh_sacnn_data,'')
  }else if(obj.event=='batch_edit'){
    var check_data=table.checkStatus('storage_table')
    if (check_data.data.length==0){
      layer.msg('请选择需要编辑的行')
      return false
    }
    var all_data={'current_data':t_data,'chose_data':check_data.data}
    edit_storage(all_data,'batch')
  }else if(obj.event=='all_edit'){
    edit_storage(t_data,'all')
  }else if(obj.event=='add_cols'){
    var fields=obj.config.cols[0]
    var new_cols={}
    fields.forEach((item)=>{
      if(item.field){
        var value=''
        if(item.field=='in_way'){
          value='manual'
        }
        new_cols[item.field]=value
      }
    })
    new_cols['index']=t_data.length+1
    t_data.push(new_cols)
    storage_table(t_data)
  }else if(obj.event=='delete'){
    var check_data=table.checkStatus('storage_table')
    if (check_data.data.length==0){
      layer.msg('请选择需要删除的行')
      return false
    }
    layer.confirm("确定删除行？",
              {btn: ['是', '否']}, 
              function (index) {
                var del_cols=[]
                var new_cols=[]
                check_data.data.forEach(item=>{
                  del_cols.push(item.index)
                })
                t_data.forEach(item=>{
                  if(del_cols.indexOf(item.index)==-1){
                    new_cols.push(item)
                  }
                })
                storage_table(new_cols)
                console.log(check_data)
                layer.close(index)
              }
              ,function(){
                console.log('no')
                layer.closeAll()
              });
    
  }
})

// 出库操作
var handle_out_scann=function(data){
  asset=data.asset
  var storage_data=data.storage_data
  var store_asset=[]
  var value=[]
  if(current_warehouse!=''){
    asset.forEach(item=>{
      if(item.status=='in_storage' && item.location==current_warehouse){
        store_asset.push(item)
      }
    })
  }else{
    asset.forEach(item=>{
      if(item.status=='in_storage'){
        store_asset.push(item)
      }
    })
  }
  transfer_func(store_asset,storage_data)
  form.val('out_sotrage_form',{'scann_id':JSON.stringify(storage_data)})
  fill_option(person,'handle_person_list','name')
}

// 出库操作
var out_warehouse=function(kind){
  var out_kind={'out_storage':'库存资产出库','lend':'库存资产领用'}
  layer.open({
    type: 1
    ,title:'<p style="font-size:20px;color:green">'+out_kind[kind]+'</p>'
    ,area:['68%','85%']
    ,offset:'100px'  
    ,id: 'out_storage_panle_id' //防止重复弹出
    ,content:$('#out_storage_panle')
    ,shade: 0 //不显示遮罩
    ,success:function(){
      post_info_ajax({'warehouse_name':current_warehouse,'kind':'out_storage'},ajax_url,'get_scann_data',handle_out_scann,'')
    }
    ,btn:['提交','刷新','关闭']
    ,btn1:function(d){
      var res=transfer.getData('out_storage_id')
      var out_storage_data=form.val('out_sotrage_form')
      var handle_person=out_storage_data.person
      var person_check=filter_func(person,'name',handle_person)
      if(person_check.length==0){
        layer.msg('请重新选择人员')
        return false
      }
      if(res.length==0){
        layer.msg('请选择需要出库的资产')
        return false
      }
      var asset_list=[]
      res.forEach(item=>{
        asset_list.push(item.value)
      })
      out_storage_data['asset_list']=asset_list
      out_storage_data['out_kind']=kind
      console.log(out_storage_data)
      post_info_ajax(out_storage_data,ajax_url,'out_storage',table_show)
    }
    ,btn2:function(d){
      post_info_ajax({'warehouse_name':current_warehouse,'kind':'out_storage'},ajax_url,'get_scann_data',handle_out_scann,'')
      return false
    }
  })
}

//穿梭框渲染
var transfer_func=function(data,checked=[],title=['库存', '待出库'],div='out_storage'){
  var elem='#'+div
  var elem_id=div+'_id'
  transfer.render({
    elem: elem
    ,id:elem_id
    ,data: data
    ,value:checked
    ,title: title
    ,height:620
    ,width:540
    ,showSearch: true
    ,parseData: function(res){
      return {
        "value": res.unique_id //数据值
        ,"title": '品类：'+res.kind+',名称:'+ res.name+',编号:'+ res.unique_id+',位置:'+ res.location+',标签:'+ res.device_id//数据标题
        ,"disabled": ''  //是否禁用
        ,"checked": '' //是否选中
      }
    }
  })
}


// 归还操作
var handle_back_scann=function(data){
  asset=data.asset
  var storage_data=data.storage_data
  var store_asset=[]
  var value=[]
  if(current_warehouse!=''){
    asset.forEach(item=>{
      if(item.status=='lend' && item.location==current_warehouse){
        store_asset.push(item)
      }
    })
  }else{
    asset.forEach(item=>{
      if(item.status=='lend'){
        store_asset.push(item)
      }
    })
  }
  transfer_func(store_asset,storage_data,['借出资产', '归还'],'back_storage')
  form.val('back_storage_form',{'scann_id':JSON.stringify(storage_data)})
  fill_option(person,'back_person_list','name')
}

// 出库操作
var back_warehouse=function(){
  layer.open({
    type: 1
    ,title:'<p style="font-size:20px;color:green">领用资产归还</p>'
    ,area:['68%','85%']
    ,offset:'100px'  
    ,id: 'back_storage_panle_id' //防止重复弹出
    ,content:$('#back_storage_panle')
    ,shade: 0 //不显示遮罩
    ,success:function(){
      post_info_ajax({'warehouse_name':current_warehouse,'kind':'back_storage'},ajax_url,'get_scann_data',handle_back_scann,'')
    }
    ,btn:['提交','刷新','关闭']
    ,btn1:function(d){
      var res=transfer.getData('back_storage_id')
      var back_storage_data=form.val('back_storage_form')
      console.log(back_storage_data)
      var handle_person=back_storage_data.person
      var person_check=filter_func(person,'name',handle_person)
      if(person_check.length==0){
        layer.msg('请重新选择人员')
        return false
      }
      if(res.length==0){
        layer.msg('请选择需要归还的资产')
        return false
      }
      var asset_list=[]
      res.forEach(item=>{
        asset_list.push(item.value)
      })
      back_storage_data['asset_list']=asset_list
      console.log(back_storage_data)
      post_info_ajax(back_storage_data,ajax_url,'back_storage',table_show)
    }
    ,btn2:function(d){
      post_info_ajax({'warehouse_name':current_warehouse,'kind':'back_storage'},ajax_url,'get_scann_data',handle_back_scann,'')
      return false
    }
  })
}





// 获取设备数据表格选中的数据，弹出用户结构弹窗，选择转移的目标用户，提交后台处理
$('.device_deliver').click(function(){
  var device_check=table.checkStatus('base_info_manage_table').data
  if(device_check.length===0){
    layer.msg('请选择需要转移的设备')
    return false
  }
  // 渲染用户树形结构
  tree.render({
    elem: '#user_tree'
    ,data: user_tree
    ,showCheckbox: false  //是否显示复选框
    ,id: 'id'
    ,onlyIconControl:true
    ,isJump: false //是否允许点击节点时弹出新窗口跳转
    ,text: {
            defaultNodeName: '未命名' //节点默认名称
            ,none: '无数据' //数据为空时的提示文本
          }   
    ,click: function(obj){
      var data = obj.data;  //获取当前点击的节点数据
      console.log(data)
      if(data['name']=='all'){
        layer.msg('该选择无效！')
        return false
      }
      $('#dev_to_user').val(data['name'])
    }

  });
  // 加载所有下级用户名称
  for(var i=0;i<user_data.length;i++){
    var user_name_option='<option>'+user_data[i]['visible_name']+'</option>'
    $('#deliver_target').append(user_name_option)
  }
// 弹窗，展示用户树形结构，选择用户，提交后台
  layer.open({
        type: 1
        ,title:'设备转移'
        ,area:['400px','500px']
        ,offset:'100px'  //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
        ,id: 'device_deliver' //防止重复弹出
        ,content:$('#device_deliver_div')
        ,btn: ['提交','关闭']
        ,shade: 0 //不显示遮罩
        ,btn1: function(){
            var result={};
            var target_user=$('#dev_to_user').val()
            if (target_user==''){
              layer.msg('请选择目标用户')
              return false
            }
            var devliver_obj=[]
            for(var i=0;i<device_check.length;i++){
              if(device_check[i]['owner']!==result['target_user']){
                devliver_obj.push(device_check[i]['name'])
              }
            }
            result['name']=devliver_obj
            result['target_user']=target_user

            layer.confirm('确定转移到'+target_user, function (index) {
              console.log(result)
              base_info_ajax(result,ajax_url,'change_owner')

            })
        }
        ,btn2: function(){
          layer.closeAll();
        }
      });

})


// 监听 导入 按钮
$('#device_input_btn').click(function(){
  var input = document.createElement('input');
  input.type = 'file';

  input.addEventListener('change', function() {
    var file = this.files[0];
    var reader = new FileReader();

    reader.onload = function (e) {
      var data = e.currentTarget.result;
      var wb = XLSX.read(data,{type:'binary'})
      var j_data = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]])
      console.log(j_data)
      if(j_data[0]['__EMPTY']!=null){
        layer.msg('表格出错')
        return false
      }

      var batch_add_data=[];
      for(var i=0;i<j_data.length;i++){
         if(j_data[i]['name']!==0 && j_data[i]['name']!=='0'  && j_data[i]['name']!=='' && j_data[i]['name']!=null){
          var res=package_data(j_data[i])
          if(res.device_id=='0' | res.device_id==0){
            res.device_id=''
          }
          batch_add_data.push(res)
         }  
      }

      layer.confirm('确定导入'+batch_add_data.length.toString()+'个品类信息', function (index) {
        // console.log(batch_add_data)
        base_info_ajax(batch_add_data,ajax_url,'add_from_file')
      })
    }
    reader.readAsBinaryString(file)
  })

    input.click();
})


// 监听设备搜索操作
form.on('submit(data-search-btn)', function (data) {

  var search_key = data.field['search_key'];
  if(search_key!==''){
    // 调用index页面搜索函数
    var search_result=search_func(asset,search_key,'unique_id','name')
    // 表格重载
    table_show(search_result)
  }else{
    table_show(asset)
  }
  return false;
});

</script>